<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use App\Models\Books;
use Illuminate\Support\Facades\Mail;

class SendMailQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;
    /**
     * Create a new job instance.
     * @param $data
     * @return void
     */
    public function __construct($data = [])
    {
        //

        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $data = $this->data;


        $email = $data['email'];
        $title  = $data['title'];

        if(empty($email)){
            echo "param error";
            return;
        }
        Mail::raw($data['content'], function ($message) use ($email,$title) {
            $message ->to($email)->subject($title);
        });
        if(!count(Mail::failures()) < 1){
            Log::info('邮件发送失败：'.json_encode($data));
        }else{
            Log::info('邮件发送成功：'.json_encode($data));
        }
    }
}
